/*
 * Copyright 2016-2021 Pnoker. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xj.driver.mqtt.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xj.common.bean.point.PointValue;
import com.xj.common.sdk.service.DriverService;
import com.xj.common.sdk.service.PointValueService;
import com.xj.driver.mqtt.service.StateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @author pnoker
 */
@Slf4j
@Service
public class StateServiceImpl implements StateService {

    @Resource
    private DriverService driverService;

    @Resource
    private PointValueService pointValueService;
    @Override
    public Boolean doChargeState(long deviceId,String chargeMsg) {
        JSONObject jo = (JSONObject)JSON.parse(chargeMsg);
        List<PointValue> listPointV= new ArrayList<>();
       /* jo.keySet().forEach(msg_key->{
            String key = msg_key.toString();
            String value = jo.getString(msg_key);
            listPointV.add(pointValueService.getPointValue(deviceId,key,value,false,""));
        });*/
        int SOC = jo.getInteger("SOC");
        long chgTime = jo.getLong("chgTime");
        double chgOutputVolt = jo.getDouble("chgOutputVolt");
        double chgOutputCurr = jo.getDouble("chgOutputCurr");
        long remainderChgTime = jo.getLong("remainderChgTime");
        int chargeState = jo.getInteger("chargeState");
        listPointV.add(pointValueService.getPointValue(deviceId,"SOC",SOC,false,""));
        listPointV.add(pointValueService.getPointValue(deviceId,"chgTime",chgTime,false,""));
        listPointV.add(pointValueService.getPointValue(deviceId,"chgOutputVolt",chgOutputVolt,false,""));
        listPointV.add(pointValueService.getPointValue(deviceId,"chgOutputCurr",chgOutputCurr,false,""));
        listPointV.add(pointValueService.getPointValue(deviceId,"remainderChgTime",remainderChgTime,false,""));
        listPointV.add(pointValueService.getPointValue(deviceId,"chargeState",chargeState,false,""));
        driverService.pointValueSender(listPointV);
        return true;
    }
}
